﻿using Microsoft.AspNetCore.SignalR;

namespace Iot.Hubs
{
    public class MessageHub : Hub
    {
        private readonly ILogger<MessageHub> _logger;

        public const string HubUrl = "/chathub";

        public MessageHub(ILogger<MessageHub> logger)
        {
            _logger = logger;
        }

        public async Task SendMessage(string receive, string message)
        {
            await Clients.All.SendAsync(receive, message);
        }

        public override Task OnConnectedAsync()
        {
            _logger.LogError($"{Context.ConnectionId} connected");
            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception? exception)
        {
            _logger.LogError($"Disconnected {exception?.Message} {Context.ConnectionId}");
            return base.OnDisconnectedAsync(exception);
        }
    }
}
